//
//  TJEditTextController.m
//  BaoDian
//
//  Created by mac on 2024/8/17.
//

#import "TJEditTextController.h"
#import "QMUITextView.h"
#import "TJButton.h"

@interface TJEditTextController ()

@property (nonatomic, weak) UIView *contentView;
@property (nonatomic, weak) UIView *maskView;
@property (nonatomic, weak) QMUITextView *textView;

@end

@implementation TJEditTextController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.view.backgroundColor = UIColor.clearColor;
    
    UIButton *maskView = [[UIButton alloc] initWithFrame:self.view.bounds].addTo(self.view);
    maskView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    _maskView = maskView;
    
    UIView *contentView = [UIView addToView:maskView];
    contentView.backgroundColor = UIColorMake(0x1E1F23);
    _contentView = contentView;
    contentView.addCornerRadius(8);
    CGSize contentSize = CGSizeMake(KScreenWidth - 32, 260);
    contentView.frame = CGRectMake((KScreenWidth - contentSize.width) * 0.5, (KScreenHeight - contentSize.height) * 0.5 - 50, contentSize.width, contentSize.height);
    
    UILabel *textLabel = [UILabel lableWithSuperView:contentView font:UIFontMake(16) color:UIColor.whiteColor title:@"编辑文案" textAlignment:NSTextAlignmentCenter];
    textLabel.addCornerRadius(9);
    textLabel.backgroundColor = [UIColor.blackColor colorWithAlphaComponent:0.3];
    [textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(24);
        make.centerX.offset(0);
    }];
 
    UIView *textFieldContentView = [UIView addToView:contentView];
    textFieldContentView.addCornerRadius(8);
    textFieldContentView.backgroundColor = UIColorMake(0x2A2B31);
    [textFieldContentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(textLabel.mas_bottom).offset(16);
        make.left.right.inset(16);
        make.bottom.offset(-54);
    }];
    
    QMUITextView *textView = [QMUITextView addToView:textFieldContentView];
    textView.font = UIFontMake(14);
    textView.text = _text;
    textView.placeholder = @"请输入文案";
    textView.backgroundColor = UIColorMake(0x2A2B31);
    textView.textColor = [UIColor whiteColor];
    textView.placeholderColor = UIColor.tj_placeholderTextColor;
    [textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.inset(10);
        make.top.bottom.offset(0);
    }];
    
    UIView *bottomView = [UIView addToView:contentView];
    bottomView.backgroundColor = contentView.backgroundColor;
    [bottomView clipsToBounds];
    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.offset(0);
        make.left.right.inset(0);
        make.height.offset(54);
    }];

//    UIView *bottomLineView = [UIView addToView:bottomView];
//    bottomLineView.backgroundColor = UIColorMake(0x2A2B31);
//    [bottomLineView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.right.top.inset(0);
//        make.height.offset(lineHeight);
//    }];
// 
    TJButton *cancelButton = [TJButton buttonWithSuperView:bottomView font:UIFontMediumMake(16) color:UIColor.tj_8D9298Color title:@"取消"];
    [cancelButton addTarget:self action:@selector(didClickCancelButton) forControlEvents:UIControlEventTouchUpInside];
    [cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.inset(0);
        make.top.bottom.inset(0);
        make.right.equalTo(contentView.mas_centerX).inset(5);
    }];
    
    
    TJButton *confirmButton = [TJButton buttonWithSuperView:bottomView font:UIFontMediumMake(16) color:UIColor.whiteColor title:@"确定"];
    [confirmButton addTarget:self action:@selector(didClickConfrimButton) forControlEvents:UIControlEventTouchUpInside];
    [confirmButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.inset(0);
        make.top.bottom.inset(0);
        make.left.equalTo(contentView.mas_centerX).inset(5);
    }];
    
    dispatch_async_main_queue(^{
        [textView becomeFirstResponder];
    });
    
    _textView = textView;
}

- (void)didClickCancelButton {
    [self dismissViewControllerAnimated:NO completion:nil];
}

- (void)didClickConfrimButton {
    [self dismissViewControllerAnimated:NO completion:^{
        if (self.complete) {
            self.complete(self.textView.text);
        }
    }];
}


- (UIModalPresentationStyle)modalPresentationStyle {
    return UIModalPresentationCustom;
}

 
@end
